/*
 * ChatClientSettingsBox.java
 *
 */

package chatclient;

import java.io.*;
import org.jdesktop.application.Action;

public class ChatClientSettingsBox extends javax.swing.JDialog {
    
    /** Creates new form ChatClientSettingsBox */
    public ChatClientSettingsBox(java.awt.Frame parent) throws IOException { // constructor
        super(parent);
        initComponents();
        File testFile = new File("settings.ini");
        if (!testFile.exists()) { // set default values if file doesn't exist. 
            PrintWriter outputStream = new PrintWriter(new FileWriter("settings.ini"));
            outputStream.println("localhost");
            outputStream.println("55555");
            outputStream.println("true"); 
            outputStream.flush();
            outputStream.close();
        }
        // read the data from the file. 
        BufferedReader inputStream = new BufferedReader(new FileReader("settings.ini"));
        tfServer.setText(inputStream.readLine());
        ftPort.setText(inputStream.readLine());
        chkTimeStamps.setSelected(Boolean.parseBoolean(inputStream.readLine()));
        inputStream.close();
        getRootPane().setDefaultButton(cancelButton);
    }
    @Action public void closeSettingsBox() {
        setVisible(false);
    }

    @Action public void cancelSettingsBox() throws FileNotFoundException, IOException {
        BufferedReader inputStream = new BufferedReader(new FileReader("settings.ini"));
        tfServer.setText(inputStream.readLine());
        ftPort.setText(inputStream.readLine());
        chkTimeStamps.setSelected(Boolean.parseBoolean(inputStream.readLine())); 
        inputStream.close();
        closeSettingsBox();
    }
    
    @Action public void defaultSettingsBox() throws FileNotFoundException, IOException {
        tfServer.setText("localhost");
        ftPort.setText("55555");
        chkTimeStamps.setSelected(true); 
    }
        
    @Action public void saveSettings() throws IOException { // write to file. 
        PrintWriter outputStream = new PrintWriter(new FileWriter("settings.ini"));
        outputStream.println(tfServer.getText());
        outputStream.println(ftPort.getText());
        outputStream.println(chkTimeStamps.isSelected()); 
        outputStream.flush();
        outputStream.close();
        ChatClientView.readSettings(); // refresh new options. 
        closeSettingsBox();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cancelButton = new javax.swing.JButton();
        lblServer = new javax.swing.JLabel();
        lblPort = new javax.swing.JLabel();
        tfServer = new javax.swing.JTextField();
        defaultButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        ftPort = new javax.swing.JFormattedTextField();
        chkTimeStamps = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(chatclient.ChatClientApp.class).getContext().getResourceMap(ChatClientSettingsBox.class);
        setTitle(resourceMap.getString("settingsBox.title")); // NOI18N
        setModal(true);
        setName("settingsBox"); // NOI18N
        setResizable(false);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(chatclient.ChatClientApp.class).getContext().getActionMap(ChatClientSettingsBox.class, this);
        cancelButton.setAction(actionMap.get("cancelSettingsBox")); // NOI18N
        cancelButton.setMnemonic('c');
        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N

        lblServer.setDisplayedMnemonic('r');
        lblServer.setLabelFor(tfServer);
        lblServer.setText(resourceMap.getString("lblServer.text")); // NOI18N
        lblServer.setName("lblServer"); // NOI18N

        lblPort.setDisplayedMnemonic('p');
        lblPort.setLabelFor(ftPort);
        lblPort.setText(resourceMap.getString("lblPort.text")); // NOI18N
        lblPort.setName("lblPort"); // NOI18N

        tfServer.setText(resourceMap.getString("tfServer.text")); // NOI18N
        tfServer.setName("tfServer"); // NOI18N

        defaultButton.setAction(actionMap.get("defaultSettingsBox")); // NOI18N
        defaultButton.setMnemonic('d');
        defaultButton.setText(resourceMap.getString("defaultButton.text")); // NOI18N
        defaultButton.setName("defaultButton"); // NOI18N

        saveButton.setAction(actionMap.get("saveSettings")); // NOI18N
        saveButton.setMnemonic('s');
        saveButton.setText(resourceMap.getString("saveButton.text")); // NOI18N
        saveButton.setName("saveButton"); // NOI18N

        ftPort.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        ftPort.setText(resourceMap.getString("ftPort.text")); // NOI18N
        ftPort.setName("ftPort"); // NOI18N
        ftPort.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                ftPortFocusLost(evt);
            }
        });

        chkTimeStamps.setMnemonic('t');
        chkTimeStamps.setSelected(true);
        chkTimeStamps.setText(resourceMap.getString("chkTimeStamps.text")); // NOI18N
        chkTimeStamps.setName("chkTimeStamps"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblServer)
                            .addComponent(lblPort))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(ftPort)
                            .addComponent(tfServer, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(defaultButton, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(saveButton))
                    .addComponent(chkTimeStamps))
                .addContainerGap(22, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblServer)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblPort))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tfServer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ftPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(chkTimeStamps)
                .addGap(153, 153, 153)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(defaultButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ftPortFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_ftPortFocusLost
        if (ftPort.getText().length() > 5)
            ftPort.setText(ftPort.getText().substring(0, 5));
}//GEN-LAST:event_ftPortFocusLost

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    public static javax.swing.JCheckBox chkTimeStamps;
    private javax.swing.JButton defaultButton;
    public static javax.swing.JFormattedTextField ftPort;
    private javax.swing.JLabel lblPort;
    private javax.swing.JLabel lblServer;
    private javax.swing.JButton saveButton;
    public static javax.swing.JTextField tfServer;
    // End of variables declaration//GEN-END:variables
}
